package diploma.kisel.modelas.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import diploma.kisel.hibernate.util.HibernateUtil;
import diploma.kisel.modelas.domain.Application;
import diploma.kisel.modelas.repository.api.ApplicationRepository;

public class ApplicationRepositoryImpl implements ApplicationRepository{
	
	private EntityManager em = HibernateUtil.getEm();

	@Override
	public List<Application> findAll() {
		List<Application> applications= em.createNamedQuery(
				"Application.findAll", Application.class).getResultList();		
		return applications;		
	}

	@Override
	public Application findById(int id) {
		TypedQuery<Application> query = em.createNamedQuery("Application.findById", Application.class);
		query.setParameter("id", id);
		return query.getSingleResult();
	}

	@Override
	public Application save(Application application) {
		em.getTransaction().begin();
		em.persist(application);
		em.getTransaction().commit();
		return application;
	}

	@Override
	public Application update(Application application) {
		em.getTransaction().begin();
		em.merge(application);
		em.getTransaction().commit();
		return application;
	}

	@Override
	public void remove(Application application) {
		em.getTransaction().begin();
		Application mergedApplication = em.merge(application);
		em.remove(mergedApplication);			
		em.getTransaction().commit();				
	}

}
